package com.rede.didiok.common.utils.business;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.rede.didiok.common.constant.conf.RedisConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.dto.user.UserDto;
import com.rede.didiok.common.enums.UserLevelEnum;
import com.rede.didiok.common.utils.IpUtils;
import com.rede.didiok.common.utils.RedisUtil;
import com.rede.didiok.common.utils.StringUtils;

/**
 * @Author: Rede
 * @DateTime: 2023-09-07 12:03
 * @Description: didiok-user服务中相关业务的操作工具类
 */
@Component
public class UserUtil {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private FileFeignUtil fileFeignUtil;

    /**
     * 批量用户脱敏【去除敏感信息】
     */
    public List<UserDto> convertUserList(Collection<UserDto> userList) {
        List<UserDto> resultUserList = new ArrayList<>();
        userList.forEach(item -> {
            resultUserList.add(convertUser(item));
        });
        return resultUserList;
    }

    /**
     * 用户脱敏【去除敏感信息】
     */
    public UserDto convertUser(UserDto item){
        UserDto user = new UserDto();
        user.setUid(item.getUid());
        user.setAvatar(item.getAvatar());
        user.setPhotoUrl(item.getPhotoUrl());
        user.setNickname(item.getNickname());
        user.setOccupation(item.getOccupation());
        user.setSummary(item.getSummary());
        user.setCreateTime(item.getCreateTime());
        user.setLoadingValid(item.getLoadingValid());
        user.setStatus(item.getStatus());
        user.setGender(item.getGender());
        user.setUserTag(item.getUserTag());
        user.setCredits(item.getCredits());
        user.setExpValue(item.getExpValue());
        user.setUserLevel(UserLevelEnum.getLvByExpValue(item.getExpValue()).getLevel());
        user.setBackgroundFileUrl(item.getBackgroundFileUrl());
        user.setBackgroundFileUid(item.getBackgroundFileUid());

        // 过滤用户
        String ip = item.getLastLoginIp();
        String ipPossession = redisUtil.get(RedisConf.USER_IP_POSSESSION + Constants.SYMBOL_COLON + user.getUid());
        if (StringUtils.isEmpty(ipPossession) && StringUtils.isNotEmpty(ip)) {
            // ip属地
            ipPossession = IpUtils.getIpPossession(ip);
            // 将信息设置到redis中
            redisUtil.setEx(RedisConf.USER_IP_POSSESSION + Constants.SYMBOL_COLON + user.getUid(), ipPossession, 1, TimeUnit.HOURS);
        }
        user.setUserIpPossession(ipPossession);
        return user;
    }

    public void setUserAvatar(Collection<UserDto> list) {
        List<String> fileUidList = new ArrayList<>();
        list.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getAvatar())) {
                fileUidList.add(item.getAvatar());
            }
            if (StringUtils.isNotEmpty(item.getBackgroundFileUid())) {
                fileUidList.add(item.getBackgroundFileUid());
            }
        });
        Map<String, String> pictureMap = fileFeignUtil.fileUidToFileUrlMap(fileUidList);
        for (UserDto item : list) {
            //获取头像
            if (StringUtils.isNotEmpty(item.getAvatar())) {
                List<String> pictureUidsTemp = StringUtils.changeStringToString(item.getAvatar(), SysConf.FILE_SEGMENTATION);
                List<String> pictureListTemp = new ArrayList<>();
                pictureUidsTemp.forEach(picture -> {
                    if (pictureMap.get(picture) != null && pictureMap.get(picture) != "") {
                        pictureListTemp.add(pictureMap.get(picture));
                    }
                });
                if (pictureListTemp.size() > 0) {
                    item.setPhotoUrl(pictureListTemp.get(0));
                }
            }

            //获取图片
            if (StringUtils.isNotEmpty(item.getBackgroundFileUid())) {
                item.setBackgroundFileUrl(pictureMap.get(item.getBackgroundFileUid()));
            }
        }
    }
}
